#include "Connection.hpp"
#include <iostream>
#include "Socket.hpp"
#include "IO_Processer.hpp"
#include<errno.h>


class Listener : public Connection
{
public:
    Listener(uint16_t _port,ft fts) : _listensockfd(std::make_unique<TcpSocket>())
    {
        _listensockfd->BulidSocket(_port);
        _sockfd = _listensockfd->SockFd();
        SetEvents(EPOLLIN | EPOLLET);
        _ft = fts;
    }

    void Recv()
    {
        while (1)
        {
            int _sockfd = _listensockfd->Accept(&_Client);
            if (_sockfd < 0)
            {
                if(errno == EAGAIN)
                {
                    break;
                }
                else if(errno == EINTR)
                {
                    continue;
                }
                else 
                    break;
            }

            std::shared_ptr<Connection> _connect = std::make_shared<IO_Processer>(_sockfd,_ft);
            SetNonBlock(_sockfd);
            _connect->SetEvents(EPOLLIN | EPOLLET);
            GetOwner()->AddConnection(_connect);
        }
    }

    void Send()
    {
        // none
    }

    void Expect()
    {
        // none
    }

    ~Listener()
    {
    }

private:
    std::unique_ptr<Socket> _listensockfd;
};
